/*
 * mainlist.h
 *
 *  Created on: 12/ott/2010
 *      Author: Andrea
 */

#ifndef MAINLIST_H_
#define MAINLIST_H_

#include <QtCore>
#include <QtGui>
#include <QMessageBox>

#include "flickable.h"
#include "typedefflick.h"

class MainList : public QWidget, public Flickable
{
    Q_OBJECT

public:
    MainList(QWidget *parent = 0)
            : QWidget(parent) {
    	setGenericFont();
    	m_costante = 5;
        m_offset = 0;
        m_height = QFontMetrics(font()).height() + m_costante;
        m_highlight = -1;
        m_selected = -1;
        m_scrollbar_width = 10;

        m_colorNames << QString::fromUtf8("Nuovo Messaggio")
		<< QString::fromUtf8("Ricevuti")
		<< QString::fromUtf8("Bozze")
		<< QString::fromUtf8("Inviati")
		<< QString::fromUtf8("Rubrica")
		<< QString::fromUtf8("Statistiche")
		<< QString::fromUtf8("Ricevuti")
		<< QString::fromUtf8("Bozze")
		<< QString::fromUtf8("Inviati")
		<< QString::fromUtf8("Rubrica")
		<< QString::fromUtf8("Statistiche")
		<< QString::fromUtf8("Ricevuti")
		<< QString::fromUtf8("Bozze")
		<< QString::fromUtf8("Inviati")
		<< QString::fromUtf8("Rubrica")
		<< QString::fromUtf8("Statistiche")
		<< QString::fromUtf8("Ricevuti")
		<< QString::fromUtf8("Bozze")
		<< QString::fromUtf8("Inviati")
		<< QString::fromUtf8("Rubrica")
		<< QString::fromUtf8("Statistiche")
		<< QString::fromUtf8("Impostazioni");
 
    	iLIcone << QString::fromUtf8("1.png")
		<< QString::fromUtf8("2.png")
		<< QString::fromUtf8("1.png")
		<< QString::fromUtf8("2.png")
		<< QString::fromUtf8("1.png")
		<< QString::fromUtf8("2.png")
		<< QString::fromUtf8("2.png")
		<< QString::fromUtf8("1.png")
		<< QString::fromUtf8("2.png")
		<< QString::fromUtf8("1.png")
		<< QString::fromUtf8("2.png")
		<< QString::fromUtf8("2.png")
		<< QString::fromUtf8("1.png")
		<< QString::fromUtf8("2.png")
		<< QString::fromUtf8("1.png")
		<< QString::fromUtf8("2.png")
		<< QString::fromUtf8("2.png")
		<< QString::fromUtf8("1.png")
		<< QString::fromUtf8("2.png")
		<< QString::fromUtf8("1.png")
		<< QString::fromUtf8("2.png")
   		<< QString::fromUtf8("1.png");
    	
        setAttribute(Qt::WA_OpaquePaintEvent, true);
        setAttribute(Qt::WA_NoSystemBackground, true);

        setMouseTracking(true);
        Flickable::setAcceptMouseClick(this);
    }

private:
    void setGenericFont() {
        f.setFamily( "times" );
        f.setPointSize( 11 );
        f.setWeight( QFont::Bold );
        setFont(f);
    }
protected:
    // reimplement from Flickable
    virtual QPoint scrollOffset() const {
        return QPoint(0, m_offset);
    }

    // reimplement from Flickable
    virtual void setScrollOffset(const QPoint &offset) {
        int yy = offset.y();
        if (yy != m_offset) {
            m_offset = qBound(0, yy, m_height * m_colorNames.count() - height());
            update();
        }
    }

protected:
    void paintEvent(QPaintEvent *event) {
        QPainter p(this);
        p.setFont(f);
        p.fillRect(event->rect(), Qt::white);
        m_pix.load("\\JackSMS\\" + iLIcone[0]);
        p.drawPixmap(event->rect(), m_pix);
        int start = m_offset / m_height;
        int y = start * m_height - m_offset;
        if (m_offset <= 0)
        	{
            start = 0;
            y = -m_offset;
        	}
        int end = start + height() / m_height + 1;
        if (end > m_colorNames.count() - 1)
            end = m_colorNames.count() - 1;
        for (int i = start; i <= end; ++i, y += m_height)
        	{
			p.setBrush(Qt::NoBrush);
			p.setPen(Qt::black); // Always write black
			if (i == m_highlight)
				{
				p.fillRect(0, y, width() - m_scrollbar_width, m_height, QColor(0, 64, 128, 100 /* alpha */));
				p.setPen(Qt::white); // If is highlighted i write white
				}
			if (i == m_selected)
				{
				p.fillRect(0, y, width() - m_scrollbar_width, m_height, QColor(0, 128, 240, 100 /* alpha */));
				p.setPen(Qt::white); // If is selected i write white
				}

#warning "Path JackSMS"
			m_pix.load("\\JackSMS\\" + iLIcone[i]);
			p.drawPixmap(2, y, m_height, m_height, m_pix);
			// p.drawText(m_height + 2, y, width(), m_height, Qt::AlignVCenter, m_colorNames[i]);
			p.drawText(5 + m_height, y, width(), m_height, Qt::AlignVCenter,
					m_colorNames[i]);
			
			// Scrollbar
			p.setBrush(Qt::black);
			int area_visibile = height() - m_height/* - m_costante */;
			int area_totale = m_colorNames.count() * m_height;

			float height_scrollbar = 
					/* Altezza visibile */						/* % = Alt. visibile / Alt. totale * 100 */
					(area_visibile) / 100 * ( ((float)area_visibile / (area_totale)) * 100) ;
			float spostamento_scroll = area_visibile / m_colorNames.count();
			p.fillRect(width() - m_scrollbar_width, i * spostamento_scroll, width(), height_scrollbar, QColor(128, 128, 128, 200 /* alpha */));
			// Debug
			// p.drawText(QPointF(200, 45), QString::number(height_scrollbar));

			}
        p.end();
    }

    void keyReleaseEvent(QKeyEvent *event) {
        if (event->key() == Qt::Key_Down) {
            m_offset += 20;
            event->accept();
            update();
            return;
        }
        if (event->key() == Qt::Key_Up) {
            m_offset -= 20;
            event->accept();
            update();
            return;
        }
    }

    void mousePressEvent(QMouseEvent *event) {
        Flickable::handleMousePress(event);
        if (event->isAccepted())
            return;

        if (event->button() == Qt::LeftButton) {
            int y = event->pos().y() + m_offset;
            int i = y / m_height;
            if (i != m_highlight) {
                m_highlight = i;
                m_selected = -1;
				// TODO: Funzione numerica
                // QMessageBox::information(this, "title", QString::number(i));
                update();
            }
            event->accept();
        }
    }

    void mouseMoveEvent(QMouseEvent *event) {
        Flickable::handleMouseMove(event);
    }

    void mouseReleaseEvent(QMouseEvent *event) {
        Flickable::handleMouseRelease(event);
        if (event->isAccepted())
            return;

        if (event->button() == Qt::LeftButton) {
            m_selected = m_highlight;
			// TODO: Funzione numerica
            QMessageBox::information(this, "title", QString::number(m_highlight));
            event->accept();
            update();
        }
    }

private:
    int m_offset;
    int m_height;
    int m_highlight;
    int m_selected;
    int m_costante;
    int m_scrollbar_width;
    QStringList m_colorNames, iLIcone;
    QFont f;
    QPixmap m_pix;
};


#endif /* MAINLIST_H_ */
